<template>
  <div class="page-container">
    <div class="container">
      <h2>手机号重置密码</h2>
      <!-- 手机号输入框 -->
      <div class="input-group">
        <input type="tel" v-model="phoneNumber" placeholder="请输入手机号" required />
      </div>
      <!-- 验证码输入框和发送验证码按钮 -->
      <div class="input-group">
        <input type="text" v-model="verificationCode" placeholder="请输入验证码" required />
        <button 
          class="btn-complete" 
          @click="sendVerificationCode" 
          :disabled="!isValidPhoneNumber || isSendingCode"
        >
          {{ isSendingCode ? countdown + 's 后重试' : '发送验证码' }}
        </button>
      </div>
      <button class="btn-complete" @click="resetPassword" :disabled="!isFormValid">确认</button>
      <div class="alternative-method">
        <router-link to="/email-reset-password" class="forgot-link">使用邮箱找回密码</router-link>
      </div>
    </div>
  </div>
</template>



<script>
import axios from 'axios'  // 修改导入方式

export default {
  data() {
    return {
      phoneNumber: '',
      verificationCode: '',
      isSendingCode: false,
      countdown: 60,
      resetToken: ''
    }
  },
  computed: {
    // 验证手机号
    isValidPhoneNumber() {
      return /^1[3-9]\d{9}$/.test(this.phoneNumber)
    },
    isFormValid() {
      return this.isValidPhoneNumber && this.verificationCode
    }
  },
  methods: {
    // 发送验证码
    async sendVerificationCode() {
      if (!this.isValidPhoneNumber) {
        this.$message.error('请输入有效的手机号')
        return
      }

      this.isSendingCode = true
      this.countdown = 60

      try {
        const response = await axios.post('/api/users/send-code/', {  // 直接使用axios
          phone: this.phoneNumber,
          scene: 'reset_pwd'
        })
        
        if (response.data.status === 'success') {
          this.$message.success('验证码已发送')
        } else {
          this.$message.error(response.data.message || '验证码发送失败')
        }
      } catch (error) {
        console.error('请求出错:', error)
        this.$message.error('网络错误，请重试')
      } finally {
        const timer = setInterval(() => {
          if (this.countdown <= 0) {
            clearInterval(timer)
            this.isSendingCode = false
          } else {
            this.countdown--
          }
        }, 1000)
      }
    },

    // 重置密码
    async resetPassword() {  // 确保方法名与模板中的@click一致
      if (!this.isFormValid) {
        this.$message.error('请填写完整信息')
        return
      }
      try {
        const response = await axios.post('/api/users/check-code/', {
          phone: this.phoneNumber,
          code: this.verificationCode,
          scene: 'reset_pwd'  // 添加场景参数
        })

        if (response.data.status === 'success') {
          document.cookie = `reset_token=${response.data.token}; path=/`;
          document.cookie = `reset_phone=${this.phoneNumber}; path=/`;  // 新增保存手机号
          this.$router.push({
            path: '/password'
          })
        } else {
          this.$message.error(response.data.message || '验证码错误')
        }
      } catch (error) {
        console.error('请求出错详情:', error)
        // 优先显示后端返回的错误信息
        if (error.response && error.response.data && error.response.data.message) {
          this.$message.error(error.response.data.message)
        } else {
          this.$message.error('网络错误，请重试')
        }

      }
    }
  }
}
</script>
<style scoped>
.page-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.container {
  background-color: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  width: 300px;
  text-align: center;
}

.alternative-method {
  margin-top: 15px;
}

.forgot-link {
  color: #007bff;
  text-decoration: none;
  font-size: 14px;
}

.forgot-link:hover {
  text-decoration: underline;
}
h2 {
  margin-bottom: 20px;
}
.input-group {
  margin-bottom: 15px;
  display: flex;
  gap: 10px;
}
.input-group input {
  flex: 1;
  padding: 10px;
  font-size: 14px;
  border: 1px solid #ddd;
  border-radius: 4px;
}
.btn-complete {
  background-color: #007bff;
  color: white;
  padding: 10px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  white-space: nowrap;
  width: 100%; /* 添加这一行使按钮宽度为100% */
}

.btn-complete:hover {
  background-color: #0056b3;
}

.btn-complete:disabled {
  background-color: #cccccc;
  cursor: not-allowed;
}
</style>